package com.llj.Road_curve_measuring_system.module.input;

import android.text.Editable;
import android.text.TextWatcher;

import com.google.android.material.textfield.TextInputLayout;
import com.llj.Road_curve_measuring_system.R;
import com.llj.Road_curve_measuring_system.util.MyUtil;

/**
 * @author ：Dyj
 * @date ：Created in 2022/4/17 14:49
 * @description：TextInputLayout监听器
 * @modified By：
 * @version: 1.0
 */
public class InputTextWatcher implements TextWatcher {
    private TextInputLayout mTilCr;
    private TextInputLayout mTilTa2;
    private TextInputLayout mTilNn;
    private TextInputLayout mTilD;
    private TextInputLayout mTilEcl;
    private TextInputLayout mTilJdx;
    private TextInputLayout mTilJdy;
    private TextInputLayout mTilJdZy;


    /**
     * 构造方法
     *
     * @param mTilCr CR
     * @param mTilTa2 TA2
     * @param mTilNn NN
     * @param mTilD D
     * @param mTilEcl ECL
     * @param mTilJdx  jdx
     * @param mTilJdy jdy
     * @param mTilJdZy jdzy
     */
    public InputTextWatcher(TextInputLayout mTilCr, TextInputLayout mTilTa2, TextInputLayout mTilNn, TextInputLayout mTilD, TextInputLayout mTilEcl, TextInputLayout mTilJdx, TextInputLayout mTilJdy, TextInputLayout mTilJdZy) {
        this.mTilCr = mTilCr;
        this.mTilTa2 = mTilTa2;
        this.mTilNn = mTilNn;
        this.mTilD = mTilD;
        this.mTilEcl = mTilEcl;
        this.mTilJdx = mTilJdx;
        this.mTilJdy = mTilJdy;
        this.mTilJdZy = mTilJdZy;
    }


    /**
     * This method is called to notify you that, within <code>s</code>,
     * the <code>count</code> characters beginning at <code>start</code>
     * are about to be replaced by new text with length <code>after</code>.
     * It is an error to attempt to make changes to <code>s</code> from
     * this callback.
     *
     * @param s
     * @param start
     * @param count
     * @param after
     */
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    /**
     * This method is called to notify you that, within <code>s</code>,
     * the <code>count</code> characters beginning at <code>start</code>
     * have just replaced old text that had length <code>before</code>.
     * It is an error to attempt to make changes to <code>s</code> from
     * this callback.
     *
     * @param s
     * @param start
     * @param before
     * @param count
     */
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    /**
     * This method is called to notify you that, somewhere within
     * <code>s</code>, the text has been changed.
     * It is legitimate to make further changes to <code>s</code> from
     * this callback, but be careful not to get yourself into an infinite
     * loop, because any changes you make will cause this method to be
     * called again recursively.
     * (You are not told where the change took place because other
     * afterTextChanged() methods may already have made other changes
     * and invalidated the offsets.  But if you need to know here,
     * to mark your place and then look up from here where the span
     * ended up.
     *
     * @param s
     */
    @Override
    public void afterTextChanged(Editable s) {
        checkInput(mTilCr);
        checkInput(mTilD);
        checkInput(mTilTa2);
        checkInput(mTilNn);
        checkInput(mTilEcl);
        checkInput(mTilJdx);
        checkInput(mTilJdy);
        checkInput(mTilJdZy);
    }

    /**
     * 数据检查以及设置错误显示
     *
     * @param textInputLayout 传入对应单个layout
     */
    public static void checkInput(TextInputLayout textInputLayout) {
        if (textInputLayout != null) {
            if (textInputLayout.getEditText().getText().length() > textInputLayout.getCounterMaxLength()) {
                textInputLayout.setError(MyUtil.getApplication().getString(R.string.til_max_limit));
            } else if (textInputLayout.getEditText().getText().length() == 0) {
                textInputLayout.setError(MyUtil.getApplication().getString(R.string.til_min_limit));
            } else {
                textInputLayout.setError(null);
            }
        }
    }
}
